package com.hc.util.data;


import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.hc.commonData.SysCommonData;
import com.hc.model.EquipmentBean;
import com.hc.util.EquUtil;
import com.hc.util.MyPoint;
import com.hc.util.fillData.CoordCartesian;
import com.hc.util.fillData.CoordConver;
import com.hc.util.fillData.WalkInfo;
import com.hc.util.fillData.WalkInfoParent;
import com.hc.util.fillData.WalkInfos;
import com.hc.util.snmp.APIServer;
import com.hc.util.snmp.SNMPConstens;
import com.hc.view.path.ShowPathActivity;

public class GetStepDataThread extends Thread{
	
	private static final int TEST_INTERVAL_TIME = 50;
	
	private boolean closed = false;
	private static WalkInfos infos = new WalkInfos();
	private static LinkedList dataList = new LinkedList<MyPoint>();

	public static WalkInfos getInfos() {
		return infos;
	}

	public static LinkedList getDataList() {
		return dataList;
	}
	
	public void closed() {
		this.closed = true;
	}


	
	@Override
	public void run() {
		
		WalkInfo lastInfo = null;
		float temp_angle = 0.0f;
		float cur_angle = 0f;
		
		while(!closed)
		{
			try {
				 List<Map<String, Object>> map = APIServer.getSNMPService().getTable(SNMPConstens.EcDialWifiCommon.EcWifiSystem.EC_TEST_STEP_INFO_TABLE);
				 
				WalkInfos tempInfos = DataDeal.snmpToData(map);

				for(WalkInfoParent info: tempInfos.walkInfos){
					if(info instanceof WalkInfo){
						if(lastInfo == null){
							lastInfo = (WalkInfo)info;
						}
						temp_angle = ((WalkInfo)info).turnAngle;
						
						if(temp_angle<55&&temp_angle>-55){
							
							cur_angle = cur_angle + 0;
							
						}else if(temp_angle>=55&&temp_angle<130){
							
							cur_angle = cur_angle + 90;
							
						}else if(temp_angle>-130&&temp_angle<=-55){
							
							cur_angle = cur_angle - 90;
							
							
						}else if(temp_angle>=130&&temp_angle<220){
							
							cur_angle = cur_angle + 180;
							
						}else if(temp_angle>-220&&temp_angle<=-130){
							
							cur_angle = cur_angle - 180;
							
							
						}else if(temp_angle<=-220||temp_angle>=220){
							
							
							cur_angle = cur_angle - 0;
							
						}else {
							cur_angle += temp_angle;
						}
						CoordCartesian xyCoord = CoordConver.Angle2Cart(lastInfo.x, lastInfo.y, cur_angle+EquipmentBean.angle, ((WalkInfo)info).step_distens *SysCommonData.stepKey *EquipmentBean.mapScale);
						dataList.addLast(new MyPoint((WalkInfo)info));
						((WalkInfo) info).x=xyCoord.x;
						((WalkInfo) info).y=xyCoord.y;
						
						lastInfo = (WalkInfo)info;
					}
					
				}
				
				infos.addAll(tempInfos.walkInfos);
				
			
				
				Map<String, Object> deleteMap = new HashMap<String, Object>();
				
				for(Map<String, Object> subMap: map){
					Object o =subMap.get("1.3.6.1.4.1.36174.11.2.1.4.1.1");
					if(o!=null){
						

						deleteMap.put((SNMPConstens.EcDialWifiCommon.EcWifiSystem.EcTestStepInfoTable.EcTestStepInfoTableEntry.ECTESTSTEPINFOSTATUS+"."+o.toString()), 6);
					}
					
				}
				APIServer.getSNMPService().set(deleteMap);
				
				Thread.sleep(TEST_INTERVAL_TIME);
				
			
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		
		
		
	}
}
